const connection = require('../app/database');

class FollowService {

  // 关注一个用户
  async create(userId, friendId, nickname) {
    try {
      const statement = `INSERT INTO follow (user_id, friend_id, friend_nickname) VALUES (?, ?, ?)`;
      const result = await connection.execute(statement, [userId, friendId, nickname]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 取消关注一个用户
  async remove(userId, friendId) {
    try {
      const statement = `DELETE FROM follow WHERE user_id = ? AND friend_id = ?`;
      const result = await connection.execute(statement, [userId, friendId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 我的关注列表
  async miniList(userId) {
    try {
      const statement = `SELECT 
                          f.friend_id id,
                          f.friend_nickname nickname,
                          u.avatar_url avatar,
                          u.creed creed,
                          u.private isPrivate,
                          u.createAt createTime,
                          u.updateAt updateTime,
                          f.createAt followTime
                        FROM follow f
                        LEFT JOIN user u ON u.id = f.friend_id
                        WHERE f.user_id = ?`;
      const result = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 关注我的列表
  async otherList(userId) {
    try {
      const statement = `SELECT 
                          f.user_id id,
                          u.nickname nickname,
                          u.avatar_url avatar,
                          u.creed creed,
                          u.private isPrivate,
                          u.createAt createTime,
                          u.updateAt updateTime,
                          f.createAt followTime
                        FROM follow f
                        LEFT JOIN user u ON u.id = f.user_id
                        WHERE f.friend_id = ?`;
      const result = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }


  // 搜索我关注的用户
  async searchMineList(userId, friendNickname) {
    try {
      const statement = `
                          SELECT 
                            f.user_id id,
                            u.nickname nickname,
                            u.avatar_url avatar,
                            u.creed creed,
                            u.private isPrivate,
                            u.createAt createTime,
                            u.updateAt updateTime,
                            f.createAt followTime
                          FROM follow f
                          LEFT JOIN user u ON f.user_id = u.id
                          WHERE f.friend_id = ? AND u.nickname like '%${friendNickname}%'`;
      const result = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 搜索关注我的用户
  async searchOtherList(userId, friendNickname) {
    try {
      const statement = `
      SELECT 
        f.user_id id,
        u.nickname nickname,
        u.avatar_url avatar,
        u.creed creed,
        u.private isPrivate,
        u.createAt createTime,
        u.updateAt updateTime,
        f.createAt followTime
      FROM user u
      LEFT JOIN follow f ON f.user_id = u.id
      WHERE f.friend_id = ? AND u.nickname like '%${friendNickname}%'`;
      const result = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 判断用户是否关注
  async judge(userId, friendId) {
    try {
      const statement = `SELECT id FROM follow WHERE user_id = ? AND friend_id = ?`;
      const result = await connection.execute(statement, [userId, friendId]);
      return result[0].length > 0;
    } catch (error) {
      console.log(error)
    }
  }

}

module.exports = new FollowService();
